<?php
class Gallery_model extends Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function addToGallery($galleryArray)
    {
        foreach($galleryArray as $photo){
            $this->db->insert('gallery',$photo);
        }
        return true;
    }
    public function getGalleryById($galleryId)
    {
        $this->db->select("*");
        $this->db->from('gallery');
        $this->db->where(array('category_id' => $galleryId));
        $sql_res = $this->db->get();
        $res = $sql_res->result_array();
        return $res;
    }

    public function addGalleryAlbum($galName)
    {
        $res = $this->db->insert('gallery_albums',array('name'=>$galName));
        if($res){
            return $this->db->insert_id();
        }
        return 0;
    }

    public function updateGalleryPhoto($photoArray)
    {
        if(is_array($photoArray) && count($photoArray) >= 1){
         return $this->db->update_batch('gallery',$photoArray,'id');
        }
        return false;
    }

    public function getGalleryList()
    {
        $this->db->select("*");
        $this->db->from('gallery_albums');
        $this->db->where(array('delete' => 0));
        $sql_res = $this->db->get();
        $res = $sql_res->result_array();
        return $res;
    }
    public function getGalleryListWithPhoto()
    {
        $galleryWithPhoto = array();
        $galleryList = $this->getGalleryList();
        foreach($galleryList as $key => $gal )
        {
            $this->db->select("*");
            $this->db->from('gallery');
            $this->db->where(array('category_id' => $gal['id'], 'delete' => 0));
            $this->db->order_by('id','RANDOM');
            $this->db->limit(4);
            $sql_res = $this->db->get();
            $galleryList[$key]['photos'] =  $sql_res->result_array();
            //$galleryWithPhoto[$gal['name']] = $sql_res->result_array();
        }
        return $galleryList;
    }

    public function deleteGalleryById($id)
    {
        $this->db->update('gallery_albums',array('delete' => 1), array('id' => $id));
        $this->db->update('gallery',array('delete' => 1), array('category_id' => $id));
        $galleryName = $this->_getCategoryNameById($id);
        return $galleryName;
    }

    public function changeStatus($id)
    {
        $this->db->select("active");
        $this->db->from('gallery_albums');
        $this->db->where(array('id' =>$id,'delete' => 0));
        $sql_res = $this->db->get();
        $status = $sql_res->row()->active;
        if($status){
            $this->db->update('gallery_albums',array('active' => 0),array('id' => $id));
        }else {
            $this->db->update('gallery_albums',array('active' => 1),array('id' => $id));
        }
    }

    private function _getCategoryNameById($id)
    {
        $this->db->select("name");
        $this->db->from('gallery_albums');
        $this->db->where(array('id' => $id));
        $sql_res = $this->db->get();
        return $sql_res->row()->name;
    }
}